<div class="modal fade" id="tmsUploadModal" tabindex="-1" role="dialog" aria-labelledby="tmsUploadModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <form class="tms-upload-form" action="" method="post" enctype="multipart/form-data">
        <input type="hidden" name="struts.enableJSONValidation" value="true" />
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="tmsUploadModalLabel">上传文件</h4>
        </div>
        <div class="modal-body">
          <div class="form-group">
              <label>请选择文件</label>
              <input type="file" id="file" name="file">
              <p class="help-block">&nbsp;</p>
          </div>
          <div class="form-group">
              <label>文件名字</label>
              <input type="text" class="form-control" name="filename">
              <p class="help-block">&nbsp;</p>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
          <button type="submit" class="btn btn-primary tms-upload-form-submit" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> 正在上传">上传</button>
        </div>
      </form>
    </div>
  </div>
</div>
<script>
(function(onSucced) {
  $(document).on('submit', '.tms-upload-form', function() {
    $('.tms-upload-form-submit').button('loading');
    var formdata = new FormData($(this).get(0));
    jQuery.ajax({
      url: '${request.contextPath}/upload.action?struts.enableJSONValidation=true',
      type: 'POST',
      dataType: 'json',
      data: formdata,
      processData : false,
      contentType: false,
      complete: function(xhr, textStatus) {
        //called when complete
        $('.tms-upload-form-submit').button('reset');
      },
      success: function(data, textStatus, xhr) {
        if (data['status'] === -1) {
          alert('您需要先登录才能进行操作');
          window.location.href = "${request.contextPath}/login-index.action?redirectUrl="+encodeURIComponent(window.location.href);
        }
        if(data['fieldErrors'] != null) {
          for (var i in data['fieldErrors']) {
            $('input[name="'+i+'"]').next().html(data['fieldErrors'][i]);
          }
          return;
        }
        if (data['status'] === 1) {
          onSucced(data.data);
          $('#tmsUploadModal').modal('hide')
        }
        if (data['status'] === 0) {
          alert(data.message);
        }
      },
      error: function(xhr, textStatus, errorThrown) {
        console.log(xhr, textStatus, errorThrown);
      }
    });

    return false;
  });
})(onUploadSucced);
</script>